package xyz.hubery.aop;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.hubery.enums.CodeEnum;
import xyz.hubery.model.BaseResult;
import xyz.hubery.model.BusException;

/**
 * 统一异常处理器
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusException.class)
    public BaseResult defaultExceptionHandler(BusException e) {
        return new BaseResult(e.getCode(), e.getMsg(), null);
    }


    /**
     * 处理系统异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public BaseResult systemExceptionHandler(Exception e) {
        e.printStackTrace();
        return new BaseResult(CodeEnum.SYSTEM_ERROR.getCode(), CodeEnum.SYSTEM_ERROR.getMessage(), null);
    }

}
